package cn.jiahua.hospital.controller;

import cn.jiahua.hospital.config.OssConfig;
import cn.jiahua.hospital.constants.Constants;
import cn.jiahua.hospital.constants.ResultEnum;
import cn.jiahua.hospital.dto.*;
import cn.jiahua.hospital.pojo.*;
import cn.jiahua.hospital.service.*;
import cn.jiahua.hospital.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api("医院")
@RequestMapping("/api/back/")
public class HospitalController {
    @Autowired
    OssUtils ossUtils;

    @Autowired
    OssConfig ossConfig;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private HospitalService hospitalService;

    @Resource
    private DepartmentsService departmentsService;

    @Resource
    private HdidRelationService hdidRelationService;

    @Resource
    private IllnessService illnessService;

    @Resource
    private DoctorService doctorService;

    @Resource
    private TdepartmentsService tdepartmentsService;

    @Resource
    private YOrderService yOrderService;

    @Resource
    private ScheduleService scheduleService;

    @Resource
    private DoctorRoleService doctorRoleService;

    @Autowired
    private PayHospitalService payHospitalService;

    /*@ApiOperation(value="医院登录",notes = "医院登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "医院账号",required = true),
            @ApiImplicitParam(name = "password",value = "医院密码",required = true)
    })
    @PostMapping("/hospital/info/password")
    public Result login(String account, String password){
        boolean b = hospitalService.hospitalLogin(account, password);
        String token = null;
        if(b){
            //生成token

        }else {
            return ResultUtils.returnFail("账号或密码错误");
        }

        return ResultUtils.returnDataSuccess(token);
    }*/

    @ApiOperation(value = "修改医院详情信息", notes = "修改医院详情信息")
    @PostMapping("/hospital/modifyhospital")
    public Result modify(Hospital hospital) {
        Integer i = hospitalService.qdtxModifyHospital(hospital);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }


    @ApiOperation(value = "添加一级科室", notes = "新增科室信息")
    @PostMapping("/departments/add")
    public Result addDepartment(Departments departments) {
        Integer i = departmentsService.qdtxAddDepartments(departments);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }

    @ApiOperation(value = "添加二级科室", notes = "新增科室信息")
    @PostMapping("/tdepartments/add")
    public Result addTdepartment(TdepartmentsDto tdepartments,HttpServletRequest request) {

        Integer hospitalId = getHospitalId(request);
        Integer i = tdepartmentsService.qdtxAddTdepartments(tdepartments);
        if (i > 0) {
            HdidRelation hdidRelation = new HdidRelation();
            hdidRelation.setHospitalId(hospitalId);
            hdidRelation.setForbidden(0);
            hdidRelation.setDepartmentsId(tdepartments.getDepartmentsId());
            hdidRelation.setTdepartmentsId(tdepartments.getTdepartmentsId());
            Integer integer1 = hdidRelationService.qdtxAddHdidRelation(hdidRelation);

            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }

    @ApiOperation(value = "修改修改科室信息", notes = "修改科室信息")
    @PostMapping("/tdepartments/modify")
    public Result modifyTdepartment(TdepartmentsDto departments) {
        Integer i = tdepartmentsService.qdtxModifyTdepartments(departments);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }


    @ApiOperation(value = "修改修改科室信息", notes = "修改科室信息")
    @PostMapping("/departments/modify")
    public Result modifyDepartment(Departments departments) {
        Integer i = departmentsService.qdtxModifyDepartments(departments);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }


    @ApiOperation(value = "分页查询科室", notes = "分页模糊查询科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentsName", value = "科室名称"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")
    })
    @PostMapping("/departments/limit")
    public Result listDepartment(String departmentsName, Integer pageNo, Integer pageSize, HttpServletRequest request) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("departmentsName", departmentsName);


        Integer hospitalId = getHospitalId(request);
        map.put("hospitalId", hospitalId);
        Integer total = departmentsService.getDepartmentsCountByMap(map);
        System.out.println("-------------:" + total);
        Integer beginPos = (pageNo - 1) * pageSize;
        map.put("beginPos", beginPos);
        map.put("pageSize", pageSize);
        List<Tdepartments> tdepartmentsList = tdepartmentsService.getTdepartmentsListByMap(map);

        if (tdepartmentsList != null) {
            //page工具类
            Page<Tdepartments> page = new Page<Tdepartments>(tdepartmentsList, pageNo, pageSize, total);

            return ResultUtils.returnDataSuccess(page);
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "分页查询二级科室(新)", notes = "分页模糊查询二级科室（新）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartmentsName", value = "科室名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小"),
            @ApiImplicitParam(name = "forbidden", value = "是否禁用")
    })
    @RequestMapping("/departments/limit1")
    public Result listDepartment1(String tdepartmentsName, Integer page, Integer limit, HttpServletRequest request,String forbidden) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tdepartmentsName", tdepartmentsName);

        Integer hospitalId = getHospitalId(request);

        map.put("hospitalId", hospitalId);

        if(forbidden.equals("0")){
            map.put("forbidden", 0);
        }
        else{
            map.put("forbidden", 1);
        }


        Integer total = tdepartmentsService.getTdepartmentsCountByMap1(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);
        List<TdepartmentsDto> tdepartmentsList = tdepartmentsService.getTdepartmentsListByMap1(map);

        if (tdepartmentsList != null) {
            //page工具类
            Page<TdepartmentsDto> page1 = new Page<TdepartmentsDto>(tdepartmentsList, page, limit, total);

            return ResultUtils.returnDataSuccess(page1);
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "查询二级科室详情", notes = "查询二级科室详情")
    @ApiImplicitParam(name = "id", value = "二级科室id")
    @GetMapping("/tdepartments/select")
    public Result selectTdepartment(@RequestParam Integer id) {
        TdepartmentsDto tdepartmentsById = tdepartmentsService.getTdepartmentsById(id);
        if (tdepartmentsById != null) {
            return ResultUtils.returnDataSuccess(tdepartmentsById);
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "获取当前医院的二级科室", notes = "获取当前医院的二级科室")
    @PostMapping("/departments/getTdepartments")
    public Result getTdepartments(HttpServletRequest request) {

        Integer hospitalId = getHospitalId(request);
        List<Tdepartments> departments = tdepartmentsService.getTdepartments(hospitalId);
        if (departments != null) {
            return ResultUtils.returnDataSuccess(departments);
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "获取所有二级科室", notes = "获取所有二级科室")
    @PostMapping("/departments/getDepartments")
    public Result getTdepartments() {
        List<Tdepartments> departments = departmentsService.getDepartments();
        if (departments != null) {
            return ResultUtils.returnDataSuccess(departments);
        }
        return ResultUtils.returnFail();
    }
    /**
     * 获取当前医院id
     */
    public Integer getHospitalId(HttpServletRequest request){
        String token = request.getHeader("token");
        Hospital h = (Hospital) redisUtil.get(token);
        Integer hospitalId = h.getHospitalId();
        return hospitalId;
    }

    @ApiOperation(value = "获取当前医院所有一级科室", notes = "获取当前医院所有一级科室")
    @PostMapping("/departments/getDepartments1")
    public Result getDepartments(HttpServletRequest request) {
        Integer hospitalId = getHospitalId(request);
        List<Departments> departments = departmentsService.getDepartments1(hospitalId);
        if (departments != null) {
            return ResultUtils.returnDataSuccess(departments);
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "根据一级获取二级科室", notes = "根据一级获取二级科室")
    @PostMapping("/departments/getTdepartments1")
    public Result getTdepartments1(Integer departmentsId) {
        List<Tdepartments> departments = tdepartmentsService.getTdepartments1(departmentsId);
        if (departments != null) {
            return ResultUtils.returnDataSuccess(departments);
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "禁用二级科室", notes = "根据hospitalId和tdepartmentsId禁用科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartments", value = "二级科室id"),
            @ApiImplicitParam(name = "forbidden", value = "是否禁用"),
    })
    @GetMapping("/departments/forbidden")
    public Result forbiddenDepartment( Integer tdepartments,String forbidden,HttpServletRequest request) {
        Integer hospitalId = getHospitalId(request);
        HdidRelation hdidRelation = new HdidRelation();
        hdidRelation.setHospitalId(hospitalId);
        hdidRelation.setTdepartmentsId(tdepartments);
        if(forbidden.equals("0")){
            //禁用
            hdidRelation.setForbidden(1);
        }else {
            //启用
            hdidRelation.setForbidden(0);
        }

        Integer i = hdidRelationService.modify(hdidRelation);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "添加疾病信息", notes = "添加疾病信息")
    @PostMapping("/illness/add")
    public Result addIllness(Illness illness) {
        Integer i = illnessService.qdtxAddIllness(illness);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "修改疾病", notes = "修改疾病信息")
    @PostMapping("/illness/modify")
    public Result modifyIllness(Illness illness) {
        Integer i = illnessService.qdtxModifyIllness(illness);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "分页查询疾病", notes = "分页模糊查询科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartmentsName", value = "科室名"),
            @ApiImplicitParam(name = "illnessName", value = "疾病名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小")
    })
    @RequestMapping("/illness/limit")
    public Result listIllness(String tdepartmentsName, String illnessName, Integer page, Integer limit,HttpServletRequest request) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("illnessName", illnessName);
        map.put("tdepartmentsName", tdepartmentsName);


        Integer hospitalId = getHospitalId(request);
        map.put("hospitalId", hospitalId);
        Integer total = illnessService.getIllnessCountByMap1(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);

        List<IllnessDto> illnessList = illnessService.getIllnessListByMap1(map);

        //page工具类
        Page<IllnessDto> page1 = new Page<IllnessDto>(illnessList, page, limit, total);

        return ResultUtils.returnDataSuccess(page1);
    }

    @ApiOperation(value = "查询疾病详情", notes = "查询疾病详情")
    @ApiImplicitParam(name = "id", value = "疾病id")
    @GetMapping("/illness/select")
    public Result selectIllness(@RequestParam Integer id) {
        IllnessDto illness = illnessService.getIllnessById(id);
        if (illness != null) {
            return ResultUtils.returnDataSuccess(illness);
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "查看是否存在此账号", notes = "查看是否存在此账号")
    @ApiImplicitParam(name = "account", value = "账号")
    @PostMapping("/doctor/exist")
    public Result exist(@RequestParam String account) {
        String exist = doctorService.exist(account);
        if (exist!=null){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "添加医生", notes = "创建医生账号")
    @PostMapping("/doctor/add")
    public Result addDoctor(DoctorDto doctor, MultipartFile file, HttpServletRequest request) {
        //从token中拿到医院id
        Integer hospitalId = getHospitalId(request);
        doctor.setScore(10.0);
        doctor.setSubscribeData(0);
        Integer i = doctorService.qdtxAddDoctor(doctor);
        if (i > 0) {
            //添加到关联表
            HdidRelation hdidRelation = new HdidRelation();
            hdidRelation.setHospitalId(hospitalId);
            hdidRelation.setForbidden(0);
            hdidRelation.setDepartmentsId(doctor.getDepartmentsId());
            hdidRelation.setTdepartmentsId(doctor.getDieseaseId());
            hdidRelation.setDoctorId(doctor.getDoctorId());
            Integer integer1 = hdidRelationService.qdtxAddHdidRelation(hdidRelation);
            if(integer1<=0){
                return ResultUtils.returnFail("添加关联表信息失败");
            }
            //获取当前医生角色的接诊数
            DoctorRole doctorRole = doctorRoleService.getDoctorRoleById(doctor.getRoleId());
            Integer reservations = doctorRole.getReservations();
            //添加当前医生的排版信息（默认）
            //上午
            Schedule schedule = new Schedule();
            schedule.setAmOne(reservations);
            schedule.setAmTwo(reservations);
            schedule.setAmThree(reservations);
            schedule.setAmFour(reservations);
            schedule.setAmFive(reservations);
            schedule.setAmSix(reservations);
            schedule.setAmSeven(reservations);
            //下午
            schedule.setPmOne(reservations);
            schedule.setPmTwo(reservations);
            schedule.setPmThree(reservations);
            schedule.setPmFour(reservations);
            schedule.setPmFive(reservations);
            schedule.setPmSix(reservations);
            schedule.setPmSeven(reservations);
            schedule.setDoctorId(doctor.getDoctorId());
            schedule.setHospitalId(hospitalId);

            Integer integer2 = scheduleService.qdtxAddSchedule(schedule);
            if(integer2<=0){
                return ResultUtils.returnFail("添加排版信息失败");
            }

            //上传图片
            String filePath = "hospital/doctor/";
            String fileName=null;
            if(file!=null){
                fileName = file.getOriginalFilename();
                //1、将multipartfile转化为file
                File fileInfo = FileUtils.MultipartFileToFile(file);
                String result = ossUtils.uploadByStream(fileInfo, filePath + fileName);
            }


            doctor.setPic(filePath + fileName);

            Integer integer = doctorService.updateDoctor_pic(doctor);
            if (integer > 0) {
                return ResultUtils.returnSuccess();
            } else {
                return ResultUtils.returnFail("图片上传失败");
            }
        }
        return ResultUtils.returnFail("添加医生失败");
    }


    @ApiOperation(value = "修改医生信息", notes = "修改医生信息")
    @PostMapping("/doctor/modify")
    public Result modifyIllness(Doctor doctor) {
        System.out.println("******"+doctor.getDoctorId());
        Integer integer = doctorService.qdtxModifyDoctor(doctor);
        if (integer > 0) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }

    @ApiOperation(value = "分页查询医生", notes = "分页模糊查询医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartmentsId", value = "二级科室id"),
            @ApiImplicitParam(name = "name", value = "医生名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小")
    })
    @RequestMapping("/doctor/limit")
    public Result listDoctor(Integer tdepartmentsId, String name, Integer page, Integer limit, HttpServletRequest request) {

        //从token中拿到医院id
        Integer hospitalId = getHospitalId(request);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tdepartmentsId", tdepartmentsId);
        map.put("hospitalId", hospitalId);
        map.put("name", name);
        Integer total = doctorService.getDoctorCount(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);


        List<DoctorDto> doctorList = doctorService.getDoctorList(map);

        //page工具类
        Page<DoctorDto> page1 = new Page<DoctorDto>(doctorList, page, limit, total);

        return ResultUtils.returnDataSuccess(page1);
    }

    @ApiOperation(value = "分页查询禁用的医生", notes = "分页模糊查询禁用的医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tdepartmentsId", value = "二级科室id"),
            @ApiImplicitParam(name = "name", value = "医生名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小")
    })
    @RequestMapping("/doctor/limit1")
    public Result listDoctor1(Integer tdepartmentsId, String name, Integer page, Integer limit,HttpServletRequest request) {

        //从token中拿到医院id
        Integer hospitalId = getHospitalId(request);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tdepartmentsId", tdepartmentsId);
        map.put("hospitalId", hospitalId);
        map.put("name", name);
        Integer total = doctorService.getDoctorCount1(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);


        List<DoctorDto> doctorList = doctorService.getDoctorList1(map);

        //page工具类
        Page<DoctorDto> page1 = new Page<DoctorDto>(doctorList, page, limit, total);

        return ResultUtils.returnDataSuccess(page1);
    }

    @ApiOperation(value = "查询医生", notes = "查询医生详情")
    @ApiImplicitParam(name = "id", value = "医生id")
    @GetMapping("/doctor/select")
    public Result selectDoctor(@RequestParam Integer id) {
        DoctorDto doctor = doctorService.getDoctor(id);
        if (doctor != null) {
            return ResultUtils.returnDataSuccess(doctor);
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "禁用医生", notes = "禁用医生详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "医生id"),
            @ApiImplicitParam(name = "forbidden", value = "是否禁用0：禁用，1：启用")
    })
    @GetMapping("/doctor/forbidden")
    public Result forbiddenDoctor(@RequestParam Integer id,@RequestParam String forbidden) {
        HdidRelation hdidRelation = new HdidRelation();
        hdidRelation.setDoctorId(id);
        if(forbidden.equals("0")){
            //禁用
            hdidRelation.setForbidden(1);
        }else {
            //启用
            hdidRelation.setForbidden(0);
        }
        Integer i = hdidRelationService.modifyDoctor(hdidRelation);
        if (i > 0) {
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "分页查询当前医院订单", notes = "分页查询当前医院订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentsName", value = "二级科室名称"),
            @ApiImplicitParam(name = "illnessName", value = "疾病名称"),
            @ApiImplicitParam(name = "userName", value = "用户名称"),
            @ApiImplicitParam(name = "doctorName", value = "医生名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小")
    })
    @RequestMapping("/order/limit")
    public Result listOrder(@RequestParam(required = false) String departmentsName,@RequestParam(required = false) String illnessName,
                            @RequestParam(required = false)String userName,@RequestParam(required = false)  String doctorName,
                            @RequestParam(required = false) Integer page, @RequestParam(required = false)Integer limit,
                            HttpServletRequest request) {
        System.out.println("---------"+departmentsName);
        System.out.println("---------"+illnessName);
        System.out.println("---------"+userName);
        System.out.println("---------"+doctorName);
        //从token中拿到医院id

        Integer hospitalId = getHospitalId(request);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("departmentName", departmentsName);
        map.put("illnessName", illnessName);
        map.put("userName", userName);
        map.put("doctorName", doctorName);
        map.put("hospitalId", hospitalId);
        Integer total = yOrderService.getYOrderCountByMap_hospital(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);

        List<YOrderDto> yOrderListByMap_hospital = yOrderService.getYOrderListByMap_hospital(map);

        //page工具类
        Page<YOrderDto> page1 = new Page<YOrderDto>(yOrderListByMap_hospital, page, limit, total);

        return ResultUtils.returnDataSuccess(page1);
    }


    @ApiOperation(value = "查询当前医院的订单", notes = "查询当前医院的订单")
    @ApiImplicitParam(name = "id", value = "订单主键id")
    @GetMapping("/order/selectid")
    public Result selectHospitalOrder(Integer id,HttpServletRequest request) {
        //从token中拿到医院id
        /*String token = request.getHeader("token");
        Hospital h = (Hospital) redisUtil.get(token);
        Integer hospitalId = h.getHospitalId();*/
        Integer hospitalId = getHospitalId(request);
        YOrderDto order = yOrderService.getYOrderById_hospital(id, hospitalId);
        if (order != null) {
            return ResultUtils.returnDataSuccess(order);
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "分页查询当前医院的排班表", notes = "分页查询当前医院的排班表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorName", value = "医生名称"),
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "limit", value = "页大小")
    })
    @RequestMapping("/schedule/limit")
    public Result listschedule(String doctorName, Integer page, Integer limit,HttpServletRequest request) {

        //从token中拿到医院id
        /*String token = request.getHeader("token");
        Hospital h = (Hospital) redisUtil.get(token);
        Integer hospitalId = h.getHospitalId();*/
        Integer hospitalId = getHospitalId(request);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("doctorName", doctorName);
        map.put("hospitalId", hospitalId);
        Integer total = scheduleService.getScheduleCountByMap_hospital(map);
        Integer beginPos = (page - 1) * limit;
        map.put("beginPos", beginPos);
        map.put("pageSize", limit);

        List<ScheduleDto> scheduleListByMap_hospital = scheduleService.getScheduleListByMap_hospital(map);
        //page工具类
        Page<ScheduleDto> page1 = null;
        if (scheduleListByMap_hospital != null) {
            page1 = new Page<ScheduleDto>(scheduleListByMap_hospital, page, limit, total);
        }

        return ResultUtils.returnDataSuccess(page1);
    }



    @ApiOperation(value = "修改排班", notes = "修改排班")
    @PostMapping("/schedule/modify")
    public Result modifyHospitalOrder(Schedule schedule) {
        Integer i = scheduleService.updateSchedule_hospital(schedule);
        if (i>0) {
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "获取医院信息", notes = "获取医院信息")
    @PostMapping("/hospital/view")
    public Result hospitalView(HttpServletRequest request) {
        //Integer hospitalId = getHospitalId(request);
        Integer hospitalId = getHospitalId(request);
        Hospital hospitalById = hospitalService.getHospitalById(hospitalId);
        if(hospitalById!=null){
            return ResultUtils.returnDataSuccess(hospitalById);
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "修改医院信息", notes = "修改医院信息")
    @PostMapping("/hospital/modify")
    public Result hospitalModify(HttpServletRequest request,Hospital hospital) {
        //Integer hospitalId = getHospitalId(request);
        Integer hospitalId = getHospitalId(request);
        hospital.setHospitalId(hospitalId);
        Integer integer = hospitalService.qdtxModifyHospital(hospital);
        if(integer>0){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    @GetMapping("/payHospital")
    @ApiOperation(value="关注医院",notes = "关注医院，返回关注数")
    public Result payHospital(Integer hospitalId,Integer userId){
        Integer payNum = 1;

        Map<String,Object> map = new HashMap<>();
        map.put("doctorId",hospitalId);
        map.put("userId",userId);

        List<PayHospital> payHospitalListByMap = payHospitalService.getPayHospitalListByMap(map);
        if (payHospitalListByMap.size()>0){
            payNum = (Integer) redisUtil.get("payHospital" + hospitalId);
            Map<String,Object> map1 = new HashMap<>();
            map1.put("data",payNum);
            map1.put("msg","已关注过该医生");
            return ResultUtils.returnDataSuccess(map1);
        }
        PayHospital payHospital = new PayHospital();
        payHospital.setHospitalId(hospitalId);
        payHospital.setUserId(userId);
        payHospital.setPayHosptialCreatTime(new Date());
        Integer integer = payHospitalService.qdtxAddPayHospital(payHospital);
        if(integer<=0){
            return ResultUtils.returnFail("关注失败，请退出登录重试");
        }
        payNum = (Integer) redisUtil.get("payHospital" + hospitalId);
        if (payNum == null){
            redisUtil.set("payHospital"+hospitalId,1);
            payNum = 1;
            return  ResultUtils.returnDataSuccess(payNum);
        }
        redisUtil.incr("payHospital" + hospitalId,1);
        return  ResultUtils.returnDataSuccess(payNum+1);
    }

    @GetMapping("unPayHospital")
    @ApiOperation(value = "取消关注医院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalId",value = "医院id")
    })
    public Result unPayHospital(Integer hospitalId,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        User user = (User) redisUtil.get(token);

        Map<String ,Object> map = new HashMap<>();
        map.put("userId",user.getId());
        map.put("hospitalId",hospitalId);

        List<PayHospital> payHospitalListByMap = payHospitalService.getPayHospitalListByMap(map);

        if (payHospitalListByMap==null || payHospitalListByMap.size()<=0){
            return ResultUtils.returnFail("数据出现错误，取消关注失败", ResultEnum.FAIL_HAVE_NOT_EXIST.getCode());
        }

        PayHospital payHospital = payHospitalListByMap.get(0);
        Integer payHospitalId = payHospital.getPayHospitalId();
        Integer integer = payHospitalService.qdtxDeletePayHospitalById(payHospitalId);
        if (integer==null || integer<=0){
            return ResultUtils.returnFail("取消关注失败",ResultEnum.FAIL_HAVE_NOT_EXIST.getCode());

        }

        Integer payNum = (Integer) redisUtil.get("payHospital" + payHospitalId);
        if (payNum!=null && payNum > 0 )
        {

            redisUtil.decr("payHospital" + payHospitalId,1);
        }
        return ResultUtils.returnSuccess();
    }
}
